// pages/view-page/view-page.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: '',
    content: '',
    account: '',
    nickname: '',
    code: '',
    reply: '',
    input: '',
    sublist: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      head: app.list[app.idx].head,
      code: app.list[app.idx].code,
      content: app.list[app.idx].content,
      account: app.list[app.idx].account,
      nickname: app.list[app.idx].nickname,
    })
    var that = this;
    console.log(that.data.code);
    wx.request({
      url: 'http://39.97.114.188:8081/queryReplyListByCode',
      method:'GET',
      data:{
        'code': that.data.code
      },
      success:function(res){
        // console.log("回调函数:"+res.data)
        var resData = res.data;
        if(resData != null){
            that.setData({
              sublist: resData,
            })
            console.log(that.data.sublist);
        }else{
            wx.showToast({
                title: '网络错误',
                duration: 2000
            })
        }
      },
      fail:function(res){
        wx.showToast({
          title: '网络错误',
          duration: 2000
        })
      }
    })
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh: function(){
    // 上拉刷新
    var that = this;
    wx.request({
      url: 'http://39.97.114.188:8081/queryReplyListByCode',
      method:'GET',
      data:{
        'code': that.data.code
      },
      success:function(res){
        // console.log("回调函数:"+res.data)
        var resData = res.data;
        if(resData != null){
            that.setData({
              sublist: resData,
            })
            console.log(that.data.sublist);
        }else{
            wx.showToast({
                title: '网络错误',
                duration:2000
            })
        }
      },
      fail:function(res){
        wx.showToast({
          title: '网络错误',
          duration: 2000
        })
      }
    })
    wx.stopPullDownRefresh({
      success: (res) => {
        wx.showToast({
          title: '刷新成功',
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 输入评论
   */
  onReplyBlur: function(e){
    this.setData({
      reply: e.detail.value,
      temp: e
    })
    console.log(this.data.reply);
  },

  /**
   * 发送评论
   */
  onCommentReply: function(){
    var that = this;
    wx.request({
      url: 'http://39.97.114.188:8081/addReply',
      method: 'GET',
      data:{
        code: that.data.code,
        content: that.data.reply,
        account: app.userAccount,
        nickname: app.userNickname
      },
      success:function(res){
        var resData = res.data;
        console.log(resData);
        if(resData == true){
          wx.showToast({
            title: '评论成功',
            duration: 2000
          })
          wx.request({
            url: 'http://39.97.114.188:8081/queryReplyListByCode',
            method:'GET',
            data:{
              'code': that.data.code
            },
            success:function(res){
              // console.log("回调函数:"+res.data)
              var resData = res.data;
              if(resData != null){
                  that.setData({
                    sublist: resData,
                    input: ''
                  })
                  console.log(that.data.sublist);
              }else{
                  wx.showToast({
                      title: '网络错误',
                      duration: 2000
                  })
              }
            },
            fail:function(res){
              wx.showToast({
                title: '网络错误',
                duration: 2000
              })
            }
          })
        }else{
          wx.showToast({
            title: '发布失败！',
            duration: 2000
          })
        }
      },
      fail:function(res){
        wx.showToast({
          title: '网络错误',
          duration: 2000
        })
      }
    })
  },
})